##############################
#   Enemy    
#   by Jerry Yang      
#   UUZ0218            
#############################
import random
import skill
from bcolors import Bcolors
from character import Character
import time
import math

def EnemyName():
    head = random.randint(0xb0, 0xf7)
    body = random.randint(0xa1, 0xfe)
    val = f'{head:x} {body:x}'
    str = bytes.fromhex(val).decode('gb2312')
    return str

def EnemySpecies():
    species = ['鼠','牛','虎','兔','龙','蛇','马','羊','猴','鸡','狗','猪','猫']
    species += ['狐','狼','虫','史莱姆','怪','妖','精','鬼','魂','象','神','仙']
    name = random.choice(species)
    return name

def EnemyNameAll():
    str = EnemyName()+EnemyName()+EnemySpecies()
    return str

def EnemyInf(inf,Enemy):
    EnemyName = EnemyNameAll()
    AdventuresLevel = inf['Level']
    EnemysLevel = random.randrange(1,AdventuresLevel+1)
    Enemy['EnemyName'] = EnemyName
    Enemy['EnemyLevel'] = EnemysLevel
    Enemy['EnemyHP'] = EnemysLevel*100
    Enemy['EnemyAttack'] = random.randrange(1,100)*EnemysLevel
    print('You meet ' + EnemyName + ' !')
    time.sleep(1)
    return Enemy

def get_EnemyInformation(Enemy):
    for na,info in Enemy.items():
        print(na + ' : ' + str(info))
    time.sleep(1)

def EnemyHurt(inf,Enemy,Attack,EnemyAlive):
    blood_minute = math.ceil(Attack)
    EnemyHP = Enemy['EnemyHP']
#    print(Bcolors.OKGREEN)
    if blood_minute >= 0:
        print('Enemy get '+str(blood_minute)+' hurt!')
    elif blood_minute < 0:
        blood_minute = 0
        print('Enemy get '+str(blood_minute)+' hurt!')
    EnemyHP -= blood_minute
#    print(Bcolors.NORMALLY)
    time.sleep(1)   
    if EnemyHP <= 0:
        print('Victory!')
        inf = Character.get_exp(inf,Enemy['EnemyLevel'])
        time.sleep(1)
        EnemyAlive = False
    elif EnemyHP > 0:
        Enemy['EnemyHP'] = EnemyHP
    return(inf,Enemy,EnemyAlive)


def dosomething(inf,Skill,Enemy,EnemyAlive):
    run_Flag = False
    while True:
        print('Please choose action')
        Act = input('1:Attack,2:Skill,3:Run,else:do nothing:')
        if Act == '1':
            [inf,Enemy,EnemyAlive] = EnemyHurt(inf,Enemy,inf['Attack'],EnemyAlive)
            break
        elif Act == '2':
            skill.get_SkillInformation(Skill)
            skill_num = len(Skill)
            SC = input('Please choose a skill:')
            if int(SC) not in range(1,skill_num+1):
                print('Choose wrong! Try again:')
                time.sleep(1)
                continue
            elif int(SC) == 0:
                print('Choose wrong! Try again:')
                time.sleep(1)
                continue
            else:
                els = list(Skill.items())
                els_choose = els[int(SC)-1]
                els_choose_inf = els_choose[1]
                els2 = list(els_choose_inf.items())
                skill_m = els2[0]
                skill_a= els2[1]
                skill_magic = skill_m[1]
                skill_attack = skill_a[1]
                [inf,Flag_skill] = Character.use_magic(inf,skill_magic)
                if Flag_skill == False:
                    print('Your magic is not enough! Choose again:')
                    time.sleep(1)
                    continue
                elif Flag_skill == True:
                    [inf,Enemy,EnemyAlive] = EnemyHurt(inf,Enemy,skill_attack,EnemyAlive)
                    break
        elif Act == '3':
            print('You run!')
            time.sleep(1)
            run_Flag = True
            break
        else:
            break
    return(inf,Skill,Enemy,EnemyAlive,run_Flag)
        

def Fight(inf,Skill):
    Enemy = {}
    EnemyInf(inf,Enemy)
    EnemyAlive = True
    Magic_origin = inf['Magic']
    while EnemyAlive:
        get_EnemyInformation(Enemy)
        [inf,Skill,Enemy,EnemyAlive,run_Flag] = dosomething(inf,Skill,Enemy,EnemyAlive)
        if EnemyAlive == False:
            break
        if run_Flag == True:
            break
        elif run_Flag == False:
            inf = Character.hurt(inf, Enemy['EnemyAttack'])
    Skill = Character.get_skill(inf,Skill)
    inf['Hp'] = inf['Level']*100
    inf['Magic'] = Magic_origin
    return (inf,Skill)